<template>
    <div>
        <div>
            <el-button type="primary" @click="showDialog('insert')" v-if="menuId==='5183'">发放任务</el-button>
            <el-popover placement="bottom-start" width="1000" style="box-shadow: 0 0 8px rgba(0,0,0,.5);" trigger="click">
                <el-form :inline="true" :model="search">
                    <el-form-item label="部件名称">
                        <el-autocomplete  class="inputSuffix"  v-model="search.name" :fetch-suggestions="searchAsyncSupplier" placeholder="请输入供应商名称" @select="changeSupplier"></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="部件编码">
                        <el-input v-model="search.code"  ></el-input>
                    </el-form-item>
                    <el-button type="primary" @click="searchData" icon="el-icon-search">查询</el-button>
                </el-form>
                <el-button slot="reference" type="primary">查询</el-button>
            </el-popover>
        </div>
        <el-table :data="dataList" :highlight-current-row="true" stripe border class="table-index" :height="tableHeight" style="width: 100%;" :header-cell-style="getRowClass" v-loading='loading'>
            <el-table-column fixed type="index" label="*"  min-width="40"></el-table-column>
            <el-table-column prop="partNameName.name" label="部件名称" sortable min-width="140"></el-table-column>
            <el-table-column prop="partCode" label="部件条码" sortable min-width="140"></el-table-column>
            <el-table-column prop="partType" label="备件型号" sortable min-width="270"></el-table-column>
            <el-table-column prop="partBatchCode" label="批号" sortable  min-width="140"></el-table-column>
            <el-table-column prop="creatorName" label="入库人" sortable  min-width="80"></el-table-column>
            <el-table-column prop="createTime" label="入库时间" sortable  min-width="120"></el-table-column>
            <el-table-column fixed="right" label="操作"  align="center" min-width="160">
                <template slot-scope="scope">
                     <el-button @click="showDialog('update',scope.row)" type="text" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagination.currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"></el-pagination>

         <el-dialog :title="dialog.title" width="90%" top="30px" :visible.sync="dialog.isShow" :close-on-click-modal="false" >
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="任务信息" name="1">
                    <el-form :inline="true" class="demo-form-inline" v-model="formData" label-position="right" label-width="180px">
                    <h3 class="dialog-plate-title"><span>基本信息</span></h3>
                    <el-form-item label="部件名称">
                        <el-autocomplete
                            v-model="formData.partNameName" :fetch-suggestions="querySearchAsync" placeholder="请输入部件名称"  @select="handleSelect" :disabled="dialog.disableObj.partNameName"></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="部件条码">
                        <el-input  v-model="formData.partCode" :disabled="dialog.disableObj.partCode"></el-input>
                    </el-form-item>
                    <el-form-item label="备件型号">
                        <el-input  v-model="formData.partType" :disabled="dialog.disableObj.partType"></el-input>
                    </el-form-item>
                    <el-form-item label="批号">
                        <el-input  v-model="formData.partBatchCode" :disabled="dialog.disableObj.partBatchCode"></el-input>
                    </el-form-item>
                    <el-form-item label="浆站名称">
                        <el-autocomplete
                            v-model="formData.customerName" :fetch-suggestions="querySearchAsync1" placeholder="请输入部件名称"  @select="handleSelect1" :disabled="dialog.disableObj.customerName"></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="机器" v-if="formData.customerName">
                        <el-select v-model="formData.machine"   placeholder="请选择" :disabled="dialog.disableObj.machine">
                            <el-option v-for="item in machineList" :key="item.id" :label="item.type" :value="item.id">
                                <span style="float: left">{{ item.type }}</span>
                                <span style="float: right; color: #aaa; font-size: 12px">{{ item.stationCode }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="维修执行人">
                        <el-select v-model="formData.performer"   placeholder="请选择" :disabled="dialog.disableObj.performer">
                            <el-option v-for="item in allRepairPersons" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="创建人" v-if="dialog.title==='编辑任务'">
                        <el-input  v-model="formData.creatorName" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" v-if="dialog.title==='编辑任务'">
                        <el-input  v-model="formData.createTime" disabled></el-input>
                    </el-form-item>
                    <h3 class="dialog-plate-title"><span>任务启动</span></h3>
                    <el-form-item label="启动">
                            <el-select v-model="formData.isStart"   placeholder="请选择" :disabled="dialog.disableObj.isStart">
                                <el-option  label="未启动" :value="0"></el-option>
                                <el-option label="启动" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    <el-form-item label="启动时间">
                        <el-input  v-model="formData.startTime" disabled></el-input>
                    </el-form-item>
                    <h3 class="dialog-plate-title"><span>维修内容</span></h3>
                        <el-form-item label="处理类型">
                            <el-select v-model="formData.dealType"   placeholder="请选择" :disabled="dialog.disableObj.dealType">
                                <el-option  label="【空】" value=""></el-option>
                                <el-option  label="维修" :value="0"></el-option>
                                <el-option label="报废" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="开始维修">
                            <el-select v-model="formData.isBegin"   placeholder="请选择" :disabled="dialog.disableObj.isBegin">
                                <el-option  label="未启动" :value="0"></el-option>
                                <el-option label="启动" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="开始维修时间">
                            <el-date-picker v-model="formData.beginTime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择开始维修时间" style="width: 200px;" :disabled="dialog.disableObj.beginTime"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="开始创建时间">
                            <el-date-picker v-model="formData.beginAddTime" format="yyyy-MM-dd HH:mm:ss" type="datetime"  style="width: 200px;" disabled></el-date-picker>
                        </el-form-item>
                        <el-form-item label="维修结束">
                            <el-select v-model="formData.isEnd"   placeholder="请选择" :disabled="dialog.disableObj.isEnd">
                                <el-option  label="未结束" :value="0"></el-option>
                                <el-option label="结束" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="维修结束时间">
                            <el-date-picker v-model="formData.endTime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择维修结束时间" style="width: 200px;" :disabled="dialog.disableObj.endTime"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="维修结束创建时间">
                            <el-date-picker v-model="formData.endAddTime" format="yyyy-MM-dd HH:mm:ss" type="datetime" disabled style="width: 200px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="维修结果">
                            <el-select v-model="formData.result"   placeholder="请选择" :disabled="dialog.disableObj.result">
                                <el-option  label="未修好" :value="0"></el-option>
                                <el-option label="修好" :value="1"></el-option>
                            </el-select>
                        </el-form-item>
                </el-form>
                <div style="text-align: center">
                    <el-button @click="submitList" type="primary">确 定</el-button>
                </div>
            </el-tab-pane>
            <el-tab-pane label="物料输入" name="2" v-if="menuId!=='5183'">
                <el-button  @click="showDialog1('add')" class="slot-btn-top" type="primary" size="small">领料</el-button>
                <el-table :data="inputMateriel.list" stripe border class="table-index" :height="dialogTableHeight" style="width: 100%;" :header-cell-style="getRowClass">
                    <el-table-column fixed type="index" label="*" sortable width="40"></el-table-column>
                    <el-table-column prop="nameName" label="物料名称" sortable min-width="120"></el-table-column>
                    <el-table-column prop="code" label="物料条码" sortable min-width="120"></el-table-column>
                    <el-table-column prop="type" label="类型" sortable min-width="80"></el-table-column>
                    <el-table-column prop="batch" label="批号" sortable min-width="80"></el-table-column>
                    <el-table-column prop="num" label="数量" sortable width="80"></el-table-column>
                    <el-table-column prop="state" label="物料状态" width="120">
                             <template slot-scope="scope">
                               <el-tag type="success" v-if="scope.row.state==1">正常使用</el-tag>
                               <el-tag type="danger" v-if="scope.row.state==0">不合格</el-tag>
                               <el-tag type="info" v-if="scope.row.state==-1">报废</el-tag>
                            </template>
                        </el-table-column>
                    <el-table-column fixed="right" label="操作" sortable align="center">
                        <template slot-scope="scope">
                            <el-button @click="showDialog1('checked',scope.row)" type="text" size="small">检验</el-button>
                            <el-button @click="deleteInputMateriel(scope.row)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="维修过程" name="3" v-if="menuId!=='5183'">
                 <check-form :data="assembly.list" @submitHanld="submitHanld" @changeDisable="changeDisable" :isDisabled="assembly.disabled"></check-form>
            </el-tab-pane>
        </el-tabs>

        </el-dialog>
        <el-dialog :title="dialog1.title" width="900px" top="30px" :visible.sync="dialog1.isShow" :close-on-click-modal="false" v-dialogDrag>
            <div :style="dialogHeightPane" v-if="dialog1.title=='领料'">
                <el-form :inline="true" class="demo-form-inline" v-model="dialog1.formData" label-position="right" label-width="180px">
                    <el-form-item label="物料名称">
                        <el-autocomplete v-model="dialog1.formData.nameName" :fetch-suggestions="querySearchAsync2" placeholder="请输入部件名称"  @select="handleSelect2" ></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="物料类型">
                        <el-input  v-model="dialog1.formData.type" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="物料编码">
                        <el-input  v-model="dialog1.formData.code" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="物料批号">
                        <el-input  v-model="dialog1.formData.batch" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="物料数量">
                        <el-input  v-model="dialog1.formData.num" ></el-input>
                    </el-form-item>
                </el-form>
                <div style="text-align: center" slot="footer">
                    <el-button @click="addShRepairMaterial" type="primary">确 定</el-button>
                </div>
            </div>
            <div class="" :style="dialogHeightPane" v-else>
                 <check-form :data="inputMateriel.checkMaterial.list" @submitHanld="submitHanld" ></check-form>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import postSaleApi from "@/axios/postSale.js"
import cgbHttp from '@/axios/cgbHttp.js'
import checkForm from '@/components/page/companyManage/components/scyb/components/checkForm.vue'
export default {
    name: 'statute-file-parent',
     components:{
        checkForm
    },
    data() {
         let menuId = sessionStorage.getItem('menuId');
      return {
            menuId,
            tableHeight:document.documentElement.clientHeight - 170,
            dialogTableHeight: document.documentElement.clientHeight - 260,
            dialogHeightPane: {
                height: (document.documentElement.clientHeight - 180) + 'px',
                overflowY: 'auto'
            },
            allParts:[],
            machineList:[],
            search:{
              code:'',
              name:''
            },
            pagination:{
              currentPage:1,
              pageSize:10,
              total:0
            },
            dataList:[],
            loading:false,
            activeName:'1',
            dialog:{
                title:'',
                isShow:false,
                disableObj:{}
            },
            dialog1:{
                title:'',
                isShow:false,
                formData:{}
            },
            formData:{},
            processStateData:[],
            allRepairPersons:[],/**所有维修人员 */
            activeName:'1',
            inputMateriel:{
                list:[],
                checkMaterial:{
                    list:[]
                },
                checkMaterialId:'',
            },
            assembly:{
                list:[],
                disabled:true
            }
      }
    },
    watch:{
        'formData.isStart'(newValue, oldValue){
            let self = this;
            if(newValue===1){
                self.formData.startTime = self.timestampToTime1(new Date(),'yy-mm-dd hh:mm:ss')
            }
        }
    },
    created(){
        let self = this;
        self.getTableList();
        postSaleApi.allBjList().then(res=>{
            res.content.forEach(element => {
                element.value = element.name
            });
            self.allParts = res.content;
        })
        postSaleApi.allRepairPersons().then(res=>{
            self.allRepairPersons= res.content;
        })
    },
    methods:{
        getTableList(){
            let self = this;
            let postData={
                currentPage:self.pagination.currentPage,
                pageSize:self.pagination.pageSize,
                code:self.search.code,
                name:self.search.name===""?null:self.search.name
            }
            self.loading = true;
            switch(self.menuId){
                case '5183':
                    postData.state=0;
                    break;
                case '5184':
                    postData.state=1;
                    break;
                 case '5185':
                    postData.state=2;
                    break;
            }
            postSaleApi.shRepairTaskList(postData).then(res=>{
                 self.loading = false;
                self.dataList= res.content.records;
                self.pagination.total =res.content.total;
            },error=>{
                 self.loading = false;
            })

        },
        searchData(){
            this.pagination.currentPage = 1;
            this.getTableList()
        },
        handleSizeChange(size){
            this.pagination.pageSize = size;
            this.getTableList()
        },
        handleCurrentChange(current){
            this.pagination.currentPage = current;
            this.getTableList();
        },
        searchAsyncSupplier(queryString, callback){
            let results = queryString ?  this.allParts.filter(this.createStateFilter(queryString)) : this.allParts;
            callback(results);
        },
        changeSupplier(item){
            this.search.supplier=item.id;
        },
        createStateFilter(queryString) {
            return (state) => {
                return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) !==-1);
            };
        },
        showDialog(type,row){
            let self = this;
            self.dialog.isShow = true;
            self.dialog.title = type==='insert'?'发布任务':'编辑任务';
             if(row&&row.machine){
                self.machineList[0]= row.machineName
            }
            self.formData = type==='insert'?{}:{
                id:row.id,
                partNameName:row.partNameName.name,
                isBegin: row.isBegin,
                beginTime: row.beginTime,
                customer: row.customer,
                customerName:row.customerName.name,
                isEnd:row.isEnd,
                endTime: row.endTime,
                machine: Number(row.machine),
                partBatchCode:row.partBatchCode,
                partCode: row.partCode,
                partName: row.partName,
                partType:row.partType,
                performer: Number(row.performer),
                result:row.result,
                endAddTime:row.endAddTime,
                beginAddTime:row.beginAddTime,
                startTime:row.startTime,
                dealType:row.dealType,
                isStart:row.isStart,
                createTime:row.createTime,
                creatorName:row.creatorName
            };
            self.dialog.disableObj={
                partNameName:type==='insert'? false :true,
                partBatchCode:type==='insert'? false :true,
                partCode: type==='insert'? false :true,
                partType: type==='insert'? false :true,
                customerName:type==='insert'?false :true,
                machine:type==='insert'?false:true,
                performer:type==='insert'?false :true,
                isStart:self.menuId==='5183'?false:true,
                dealType:self.menuId==='5183'?false:true,
                isBegin:self.menuId==='5184'?false:true,
                beginTime:self.menuId==='5184'?false:true,
                isEnd:self.menuId==='5184'?false:true,
                endTime:self.menuId==='5184'?false:true,
                result:self.menuId==='5184'?false:true
            }
            self.activeName = '1'
        },
        querySearchAsync(queryString, callback){
            let results = queryString ?  this.allParts.filter(this.createStateFilter(queryString)) : this.allParts;
            callback(results);
        },
        handleSelect(item){
            this.formData.partName= item.id;
        },
        querySearchAsync1(queryString, cb){
            postSaleApi.allShCustomerInfo({name:queryString}).then(res=>{
                res.content.forEach(item=>{
                    item.value = item.name
                })
                cb(res.content);
            })
        },
        handleSelect1(item){
            let self = this;
            self.formData.customer=item.id;
            postSaleApi.machineListBgCid(self.formData.customer).then(res=>{
                self.machineList = res.content;
            })
        },
        querySearchAsync2(queryString, cb){
            let self = this;
            postSaleApi.searchMaterialStock({name:queryString}).then(res=>{
                res.content.forEach(item=>{
                    item.value = item.resourcenameName.name
                })
                cb(res.content);
            })
        },
         handleSelect2(item){
            let self = this;
            self.dialog1.formData.name=item.resourcename;
            self.dialog1.formData.code=item.taskcode;
            self.dialog1.formData.batch=item.batchnums;
            self.dialog1.formData.type=item.resourcenameName.type;
            self.dialog1.formData.kid =item.tid;

        },
        submitList(){
            let self = this;
            let type = self.dialog.title==='发布任务'?'insert':'update';
            self.formData.beginTime= self.formData.beginTime? self.timestampToTime1(self.formData.beginTime,'yy-mm-dd hh:mm:ss'):null;
            self.formData.endTime= self.formData.endTime? self.timestampToTime1(self.formData.endTime,'yy-mm-dd hh:mm:ss'):null;
            let postData={
                id:self.formData.id,
                isBegin: self.formData.isBegin,
                beginTime: self.formData.beginTime,
                customer: self.formData.customer,
                isEnd:self.formData.isEnd,
                endTime: self.formData.endTime,
                machine: self.formData.machine,
                partBatchCode: self.formData.partBatchCode,
                partCode: self.formData.partCode,
                partName: self.formData.partName,
                partType:self.formData.partType,
                performer: self.formData.performer,
                result:self.formData.result,
                endAddTime:self.formData.endAddTime,
                beginAddTime:self.formData.beginAddTime,
                startTime:self.formData.startTime,
                dealType:self.formData.dealType,
                isStart:self.formData.isStart,
            }
            postSaleApi.addUpdateShRepair(type,postData).then(res=>{
                self.dialog.isShow = false;
                self.$message.success(`${self.dialog.title}成功`);
                self.getTableList();
            });

        },
        handleClick(tab, event){
            let self = this;
                switch(self.activeName){
                case '1':
                break;
                case '2':
                   self.inputMaterielList();
                break;
                case '3':
                    self.getAssemblyList();
                break;
            }
        },
        inputMaterielList(){
            let self = this;
            // 获取输入物料列表
            postSaleApi.shRepairMaterialList(self.formData.id).then(res=>{
                self.inputMateriel.list = res.content;
            })
        },
        showDialog1(type,row){
          let self = this;
          self.dialog1.title = type==='add'?'领料':'检验';
          self.dialog1.isShow = true;
          if(type==='add'){
              self.dialog1.formData={
                    pid:self.formData.id,
                    num:1,
                    batch:'',
                    code:'',
                    type:'',
                    nameName:'',
                    name:''
                }
          }else{
             self.inputMateriel.checkMaterialId=row.id;
             self.materialListCheck();
          }
        },
        addShRepairMaterial(){
            let self = this;
            postSaleApi.addShRepairMaterial(self.dialog1.formData).then(res=>{
                self.$message.success("领料成功");
                self.dialog1.isShow = false;
                self.inputMaterielList();
            })
        },
        deleteInputMateriel(row){
            let self = this;
             this.$confirm('确认删除吗物料?', '提示', {
                    confirmButtonClass: 'el-button--warning'
                }).then(() => {
                     postSaleApi.deleteShRepairMaterial(row.id).then(res=>{
                         self.inputMaterielList()
                          self.$message.success('删除成功!')
                     },error=>{
                          self.$message.error('删除失败!')
                     })
                })
        },
        submitHanld(data){
            let self = this;
            switch(self.activeName){
                case '2':
                    postSaleApi.updateShRepairMaterialCheck(data).then(res=>{
                        self.dialog1.isShow= false;
                        self.materialListCheck();
                        self.$message.success("检验记录提交成功!");
                    })
                    break;
                case '3':
                    let loading =self.$loading({
                        lock: true,
                        text: '数据提交中',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                    postSaleApi.updateAssembly(data).then(res=>{
                        loading.close();
                        self.$message.success("装配过程检测信息录入成功！");
                        self.getAssemblyList();
                        self.assembly.disabled = true;
                    },error=>{
                        loading.close();
                        self.$message.error("装配过程检测信息录入失败！");
                    })
            }

        },
        materialListCheck(){
            let self = this;
            postSaleApi.shRepairMaterialListCheck(self.inputMateriel.checkMaterialId).then(res=>{
                res.content.forEach(el=>{
                    if(el.type==1){
                        el.value=el.value?el.value:'1';
                    }
                })
               self.inputMateriel.checkMaterial.list=res.content;
            })
        },
         /** 获取维修过程记录列表*/
        getAssemblyList(){
            let self = this;
            postSaleApi.assemblyList(self.formData.id).then(res=>{
                self.assembly.list=res.content;
            })
        },
        changeDisable(){
            this.assembly.disabled = false;
        }
    }
  }
</script>
<style scoped>
</style>
